package com.ym.b2b.complaint.controller;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ym.b2b.commons.conf.WebTokenUser;
import com.ym.b2b.commons.controller.BaseController;
import com.ym.b2b.commons.util.CheckData;
import com.ym.b2b.complaint.service.EmBComplaintService;
import com.ym.b2b.complaint.sqlbean.EmBComplaint;
import com.zc.commons.base.Page;
import com.zc.commons.base.Result;

@Controller
@RequestMapping("/complaint")
public class ComplaintController extends BaseController {

    @Autowired
    private EmBComplaintService emBComplaintService;

    // 增加投诉
    @RequestMapping("addEmBComplaint.s")
    @ResponseBody
    public Result addEmBComplaint(EmBComplaint emBComplaint, HttpServletRequest req) {
        Result result = new Result();
        WebTokenUser user = getWebTokenUser(req);
        if (null == user) {
            emBComplaint.setCreateUser("游客：" + CheckData.getIpAddr(req));
        } else {
            emBComplaint.setCreateUser(String.valueOf(user.getUserId()));
            emBComplaint.setUserName(String.valueOf(user.getUserName()));
        }
        try {
            result = emBComplaintService.addEmBComplaint(emBComplaint);
        } catch (Exception e) {
            result.setCodeResult(Result.CODE_100, e.getMessage());
        }
        return result;
    }

    // 列表查询
    @RequestMapping("searchPageEmBComplaint.s")
    @ResponseBody
    public Result searchPageEmBComplaint(EmBComplaint emBComplaint, Page<EmBComplaint> page, HttpServletRequest req) {
        Result result = new Result();
        WebTokenUser t = getWebTokenUser(req);
        emBComplaint.setCreateUser(String.valueOf(t.getUserId()));
        result = emBComplaintService.searchPageEmBComplaint(emBComplaint, page);
        return result;
    }

    // 获取单条记录内容
    @RequestMapping("searchEmBComplaint.s")
    @ResponseBody
    public EmBComplaint searchEmBComplaint(EmBComplaint emBComplaint, HttpServletRequest req) {
        EmBComplaint result = new EmBComplaint();
        result = emBComplaintService.searchEmBComplaint(emBComplaint);
        return result;
    }

    // 删除记录内容
    @RequestMapping("deleteEmBComplaint.s")
    @ResponseBody
    public Result deleteEmBComplaint(EmBComplaint emBComplaint, HttpServletRequest req) {
        Result result = new Result();
        try {
            emBComplaintService.deleteEmBComplaint(emBComplaint.getComplaintId());
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            result.setCodeResult(Result.CODE_100, e.getMessage());
        }
        return result;
    }

    // 删除多条记录内容
    @RequestMapping("deleteMultipleEmBComplaint.s")
    @ResponseBody
    public Result deleteMultipleEmBComplaint(String complaintIdList, HttpServletRequest req) {
        Result result = new Result();
        try {
            emBComplaintService.deleteMultipleEmBComplaint(complaintIdList);
        } catch (Exception e) {
            // TODO: handle exception
            result.setCode("100");
            return result;
        }
        result.setCode("200");
        return result;
    }

    // 更新记录
    @RequestMapping("updateEmBComplaint.s")
    @ResponseBody
    public Result updateEmBNews(EmBComplaint emBComplaint, HttpServletRequest req) throws DataAccessException {
        Result result = new Result();
        result = emBComplaintService.updateEmBComplaint(emBComplaint);
        return result;
    }

}
